package com.fresh.bus.chat.ctrl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;

import com.eeplat.social.openapi.renren.AuthRenRen;
import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.util.MessageUtils;

@Controller("busCtrl")
public class BusCtrl extends BaseCtrl {

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.STOP_INFO)
	public Response queryStopInfo(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String sid = data.get("sid");

		Response response = new Response(OpCodes.STOP_INFO);
		response.setData(scheduleManager.findLineByStation(sid).toArray());
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.ROOM_INFO)
	public Response queryRoomInfo(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String sid = data.get("sid");
		if (sid == null) {
			sid = (String) session.getAttribute("sid");
		}

		Response response = new Response(OpCodes.ROOM_INFO);
		response.setData(scheduleManager.findRoomByStation(sid).toArray());

		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.LINE_INFO)
	public Response queryLineInfo(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String lid = data.get("lid");

		Response response = new Response(OpCodes.LINE_INFO);
		response.setData(scheduleManager.findStationsByLine(lid).toArray());
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.CITY_INFO)
	public Response queryCityInfo(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		Response response = new Response(OpCodes.CITY_INFO);
		response.setData(scheduleManager.getCities().toArray());
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.VISIT_CITY)
	public Response visitCity(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String sid = (String) data.get("sid");
		this.userService.setUserAddress(session, sid);

		Response response = new Response(OpCodes.VISIT_CITY);
		response.setData(scheduleManager.findRoomByStation(sid).toArray());
		return response;
	}

	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.NEXT_STOP)
	public Response queryNextStop(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String lid = data.get("lid");
		String sid = data.get("sid");
		String dir = data.get("direction");

		session.setAttribute("lid", lid);

		// Response response = new Response(OpCodes.NEXT_STOP);
		String room = scheduleManager.stop(sid).get("room");
		Request req = new Request();
		Map<String, String> m = new HashMap<String, String>();
		session.setAttribute("direction", dir);
		session.setAttribute("forwardNextStop", true);
		m.put("room", room);
		req.setData(m);
		return waitTalk(session, req);
	}

	// 获取时长礼物
	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.GET_TIME_REWARD)
	public Response getTimeReward(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.GET_TIME_REWARD);
		try {
			this.wealthService.getFlower(id, 3);
			expService.update(session, data, OpCodes.GET_TIME_REWARD);
			response.setData(true);
		} catch (Exception ex) {
			response.setData(ex.getMessage());
		}
		return response;
	}

	// 排队等候
	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.WAIT_TALK)
	public Response waitTalk(Session session, Request request) {
		Response response = new Response(OpCodes.WAIT_TALK);
		try {
			Map<String, String> data = (Map<String, String>) request.getData();
			String rid = data.get("room");
			session.setAttribute("rid", rid);
			scheduleManager.enterQueue(session);

			Map<String, String> room = this.scheduleManager.room(rid);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("room", room);
			response.setData(map);
			return response;
		} catch (Exception ex) {
			log.error("Error occur...", ex);
			this.reportError(session);
		}
		return null;
	}

	// 排队等候
	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.BEGIN_WAIT)
	public Response beginWait(Session session, Request request) {
		try {
			Map<String, Object> data = (Map<String, Object>) request.getData();
			String id = session.getRole().getId();
			String question = (String) data.get("w");
			this.scheduleManager.notifyCandidate(session, question);
		} catch (Exception e) {
			log.error("beginWait failed...", e);
		}
		return null;
	}

	// 取消等候
	@SuppressWarnings("unchecked")
	@CanoeCommand(value = OpCodes.CANCEL_WAIT)
	public Response cancelWait(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		scheduleManager.leftQueue(session);
		session.removeAttribute("rid");
		Map<String, String> map = new HashMap<String, String>();

		Response response;
		response = new Response(OpCodes.CANCEL_WAIT);
		response.setData(map);

		return response;
	}
}
